import GameCfgData from "../lib/GameCfgData";
import Sdk from "../lib/Sdk";
import PlayerData from "../com/data/PlayerData";

export default class GameIcon extends Laya.Script {
    private filterAppid:string="wxbd5459b7ecc54ec2";
    private index:number=0;
    private currIndex:number=0;
    private games=[];
    constructor() { super(); }
    
    onEnable(): void {
        this.games=GameCfgData.getIns().data.games;
        if(this.filterAppid)this.filterid()
        this.updateGame();
        this.owner.on(Laya.Event.CLICK,this,this.click);
    }

    onUpdate(){
        this.index++;
        if(this.index % 300==0){
            this.currIndex++;
            if(this.currIndex>=this.games.length)this.currIndex=0;
            this.updateGame();
        }
    }

    private updateGame(){
        let tdata = this.games[this.currIndex];
        this.owner.getChildByName("icon")["texture"]=tdata.icon;
        this.owner.getChildByName("nameTxt")["text"]=tdata.name;
    }

    private click(){
        let tdata = this.games[this.currIndex];
        Sdk.getIns().openGame(tdata);
        PlayerData.getIns().setDayTaskAwardSt(4,1);
    }

    onDisable(): void {
        this.owner.off(Laya.Event.CLICK,this,this.click);
    }

    private filterid(){
        for(let i=0;i<this.games.length;i++){
            if(this.games[i].appId==this.filterAppid){
                this.games.splice(i,1);
                break;
            }
        }
    }
}